﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;
using System.Linq;
namespace QldtSdh.GUI.KhaoThi
{
    public partial class FormQuanLyDotThi_BoSungLopHocPhan : QldtSdh.GUI.Common.BaseDialog
    {
        DotThi dotThi;
        bool allowUpdate = false;
        bool isSelectedAll = true;
        int entityTobeAdded = 0;
        public event Common.TListChangedEventHandler OnChanged;
        public FormQuanLyDotThi_BoSungLopHocPhan(DotThi dotThi)
        {
            InitializeComponent();
            this.dotThi = dotThi;
            InitGrid();
            allowUpdate = false;
            LoadHocKy();
            allowUpdate = true;
            cmbHocKy_SelectedIndexChanged(this, null);

        }

        void InitGrid()
        {
            gridLopHocPhan.AutoGenerateColumns = false;
            colTenLopHocPhan.DataPropertyName = "TenLopHocPhanFull";
            colSelected.DataPropertyName = "IsSelected";
            colConLai.DataPropertyName = "SoHocVienConNo";
        }

        void LoadHocKy()
        {
            cmbHocKy.DataSource = BizHocKy.GetListHocKy();
            cmbHocKy.DisplayMember = "TenHocKyDayDu";
            cmbHocKy.ValueMember = "MaHocKyGUID";
        }

        void LoadLopHocPhan()
        {
            bdLopHocPhan.DataSource = null;
            btnSelect.Enabled = false;
            var hk = cmbHocKy.SelectedItem as HocKy;
            if (hk != null)
            {
                var ls = BizLopHocPhan.GetList(hk.MaHocKyGUID);
                List<LopHocPhan> rsLs = new List<LopHocPhan>();
                if (ls != null && ls.Count > 0)
                {
                    foreach (var item in ls)
                    {
                        if (item.SoHocVienConNo != 0)
                        {
                            item.IsSelected = true;
                            rsLs.Add(item);
                        }
                    }
                    btnThem.Enabled = btnSelect.Enabled = true;
                }
                else
                {
                    btnThem.Enabled = btnSelect.Enabled = false;
                }
                var lsHPTrongDotThi = BizDotThi_LopHocPhan.GetList(dotThi.MaDotThiGUID);
                if (lsHPTrongDotThi != null && lsHPTrongDotThi.Count > 0 && rsLs != null && rsLs.Count > 0)
                {
                    foreach (var item in lsHPTrongDotThi)
                    {
                        var hpIsExist = (from e in rsLs
                                         where e.MaLopHocPhanGUID == item.MaLopHocPhanGUID
                                         select e).FirstOrDefault();
                        if (hpIsExist != null)
                            rsLs.Remove(hpIsExist);
                    }
                }
                bdLopHocPhan.DataSource = rsLs;
            }
            else
            {
                btnThem.Enabled = btnSelect.Enabled = false;
            }
            gridLopHocPhan.DataSource = bdLopHocPhan;
        }

        private void cmbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
                LoadLopHocPhan();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            var ls = bdLopHocPhan.DataSource as List<LopHocPhan>;
            if (ls != null && ls.Count > 0)
            {
                foreach (var item in ls)
                    item.IsSelected = isSelectedAll;
            }
            if (isSelectedAll)
            {
                isSelectedAll = false;
            }
            else
            {
                isSelectedAll = true;
            }
            gridLopHocPhan.Refresh();
        }



        private void btnThem_Click(object sender, EventArgs e)
        {
            var lsHocPhan = bdLopHocPhan.DataSource as List<LopHocPhan>;
            int soLopHocPhanDuocThem = 0;
            if (lsHocPhan != null && lsHocPhan.Count > 0)
            {
                foreach (var item in lsHocPhan)
                {
                    if (item.IsSelected)
                    {
                        DotThi_LopHocPhan dtlhp = new DotThi_LopHocPhan();
                        dtlhp.MaDotThiGUID = dotThi.MaDotThiGUID;
                        dtlhp.MaLopHocPhanGUID = item.MaLopHocPhanGUID;
                        dtlhp.HinhThucThi = item.HinhThucThi;
                        dtlhp.NgayThi = DateTime.Now;
                        dtlhp.GioThi = "08h00";
                        var rs = BizDotThi_LopHocPhan.Add(dtlhp);
                        if (rs == CODE_RESULT_RETURN.MaTrung)
                            item.TinhTrang = "Đã có trong lịch thi";
                        else if (rs == CODE_RESULT_RETURN.ThanhCong)
                        {

                            item.TinhTrang = "Thành công";
                            soLopHocPhanDuocThem++;
                            entityTobeAdded++;
                        }
                    }
                }
                if (soLopHocPhanDuocThem > 0)
                {
                    OnChanged(this, null);
                    gridLopHocPhan.Refresh();
                    LoadLopHocPhan();
                }
                DialogUtils.ShowMessage(string.Format("Đã thêm {0} lớp học phần vào đợt thi thành công", soLopHocPhanDuocThem));
            }
        }

        private void FormQuanLyDotThi_BoSungLopHocPhan_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (entityTobeAdded > 0)
                DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
